﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;

namespace LynxWebLog
{
  public class Post
  {
    public int postid { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string mt_text_more { get; set; }
    public string mt_keywords { get; set; }
    public string wp_slug { get; set; }
    public string mt_basename { get; set; }
    public string mt_excerpt { get; set; }
    public string wp_author_id { get; set; }
    public string[] categories { get; set; }
    public bool IsPublished { get; set; }
    public DateTime dateCreated { get; set; }
    public string license { get; set; }

    public Post() { }

    public Post(int postid, string title, string description, string mt_text_more, string mt_keywords,
                    string wp_slug, string mt_basename, string mt_excerpt, string wp_author_id,
                    string[] categories, bool isPublished, DateTime dateCreated)
    {
        this.postid = postid;
        this.title = title;
        this.description = description;
        this.mt_text_more = mt_text_more;
        this.mt_keywords = mt_keywords;
        this.wp_slug = wp_slug;
        this.mt_basename = mt_basename;
        this.mt_excerpt = mt_excerpt;
        this.wp_author_id = wp_author_id;
        this.categories = categories;
        this.IsPublished = isPublished;
        this.dateCreated = dateCreated;
    }

    public Post(PostData p)
    {
        this.postid = p.postid;
        this.title = p.title;
        this.description = p.description;
        this.mt_text_more = p.mt_text_more;
        this.mt_keywords = p.mt_keywords;
        this.wp_slug = p.wp_slug;
        this.mt_basename = p.mt_basename;
        this.mt_excerpt = p.mt_excerpt;
        this.wp_author_id = p.wp_author_id;
        this.categories = p.categories;
        this.IsPublished = p.IsPublished;
        this.dateCreated = p.dateCreated;
        this.license = p.license;
    }

    public Post(CurrentPostData p)
    {
        this.postid = p.postid;
        this.title = p.title;
        this.description = p.description;
        this.mt_text_more = p.mt_text_more;
        this.mt_keywords = p.mt_keywords;
        this.wp_slug = p.wp_slug;
        this.mt_basename = p.mt_basename;
        this.mt_excerpt = p.mt_excerpt;
        this.wp_author_id = p.wp_author_id;
        this.categories = p.categories;
        this.IsPublished = p.IsPublished;
        this.dateCreated = p.dateCreated;
        this.license = p.license;
    }

    public Post(string xml)
    {
        XDocument doc = XDocument.Parse(xml);
        Post temp = (from item in doc.Descendants("blogPost")
                     select new Post {
                         postid = (int)item.Element("postid"),
                         title = (string)item.Element("title"),
                         description = (string)item.Element("description"),
                         mt_text_more = (string)item.Element("mt_text_more"),
                         mt_keywords = (string)item.Element("mt_keywords"),
                         wp_slug = (string)item.Element("wp_slug"),
                         mt_basename = (string)item.Element("mt_basename"),
                         mt_excerpt = (string)item.Element("mt_excerpt"),
                         wp_author_id = (string)item.Element("wp_author_id"),
                         IsPublished = (bool)item.Element("isPublished"),
                         dateCreated = (DateTime)item.Element("dateCreated")
                     }).First();
        string[] tCategories = doc.Descendants("blogPost").Elements("categories").Select(t => (string)t.Attribute("category")).ToArray();
        this.postid = temp.postid;
        this.title = temp.title;
        this.description = temp.description;
        this.mt_text_more = temp.mt_text_more;
        this.mt_keywords = temp.mt_keywords;
        this.wp_slug = temp.wp_slug;
        this.mt_basename = temp.mt_basename;
        this.mt_excerpt = temp.mt_excerpt;
        this.wp_author_id = temp.wp_author_id;
        this.categories = tCategories;
        this.IsPublished = temp.IsPublished;
        this.dateCreated = temp.dateCreated;
    }

    public string ToXml()
    {
        XDocument doc = new XDocument(
            new XElement("blogPost",
                new XElement("postid", postid),
                new XElement("title", title),
                new XElement("description", description),
                new XElement("mt_text_more", mt_text_more),
                new XElement("mt_keywords", mt_keywords),
                new XElement("wp_slug", wp_slug),
                new XElement("mt_basename", mt_basename),
                new XElement("mt_excerpt", mt_excerpt),
                new XElement("wp_author_id", wp_author_id),
                categories.ToList().Select(t => new XElement("categories", new XAttribute("category", t))),
                new XElement("isPublished", IsPublished),
                new XElement("dateCreated", dateCreated.ToString("yyyy-MM-ddTHH:mm:ss.fff"))
                ));
        return doc.ToString(SaveOptions.None);
    } 

  }

  public class CategoryInfo
  {
    public int categoryid { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string htmlUrl { get; set; }
    public string rssUrl { get; set; }

    public CategoryInfo() { }

    public CategoryInfo(int categoryid, string title, string description,
                            string htmlUrl, string rssUrl) 
    {
        this.categoryid = categoryid;
        this.title = title;
        this.description = description;
        this.htmlUrl = htmlUrl;
        this.rssUrl = rssUrl;
    }

    public CategoryInfo(CategoryData cd)
    {
        this.categoryid = cd.categoryid;
        this.title = cd.title;
        this.description = cd.description;
        this.htmlUrl = cd.htmlUrl;
        this.rssUrl = cd.rssUrl;
    }

    public CategoryInfo(string xml)
    {
        XDocument doc = XDocument.Parse(xml);
        CategoryInfo temp = (from item in doc.Descendants("categoryInfo")
                     select new CategoryInfo {
                         categoryid = (int)item.Element("categoryid"),
                         title = (string)item.Element("title"),
                         description = (string)item.Element("description"),
                         htmlUrl = (string)item.Element("htmlUrl"),
                         rssUrl = (string)item.Element("rssUrl")
                     }).First();
        this.categoryid = temp.categoryid;
        this.title = temp.title;
        this.description = temp.description;
        this.htmlUrl = temp.htmlUrl;
        this.rssUrl = temp.rssUrl;
    }

    public string ToXml()
    {
        XDocument doc = new XDocument(
            new XElement("categoryInfo",
                new XElement("categoryid", categoryid),
                new XElement("title", title),
                new XElement("description", description),
                new XElement("htmlUrl", htmlUrl),
                new XElement("rssUrl", rssUrl)
                ));
        return doc.ToString(SaveOptions.None);
    } 


  }

  public class CategoryInfoWP
  {
      public int categoryId { get; set; }
      public int parentId { get; set; }
      public string description { get; set; }
      public string categoryName { get; set; }
      public string htmlUrl { get; set; }
      public string rssUrl { get; set; }

      public CategoryInfoWP() { }

      public CategoryInfoWP(CategoryInfo ci)
      {
          this.categoryId = ci.categoryid;
          this.parentId = 0;
          this.categoryName = ci.title;
          this.description = ci.description;
          this.htmlUrl = ci.htmlUrl;
          this.rssUrl = ci.rssUrl;
      }

  }

  public class PostCategories
  {
      public string categoryName { get; set; }
      public string categoryId { get; set; }
      public bool isPrimary
      {
          get { return false; }
      }

      public PostCategories() { }

      public PostCategories(CategoryData ci)
      {
          this.categoryId = ci.categoryid.ToString();
          this.categoryName = ci.title;
      }
  }

  public class MediaObject
  {
    public string name { get; set; }
    public string type { get; set; }
    public byte[] bits { get; set; }
  }

  public class MediaObjectInfo
  {
    public string file { get; set; }
    public string url { get; set; }
    public string type { get; set; }

    public MediaObjectInfo() { }

    public MediaObjectInfo(string file, string url, string type)
    {
        this.file = file;
        this.url = url;
        this.type = type;
    }
  }

  public class UserInfo
  {
    public string userid { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string nickname { get; set; }
    public string email { get; set; }
    public string url { get; set; }
  }

  public class XmlRpcFaultInfo
  {
    public int faultCode { get; set; }
    public string faultString { get; set; }
  }
}